<?php

class Pages
{
    private static $pages = array();
    private static $rootPage = null;

    private function __construct() {}

    public static function GetRootPage()
    {
        if ( is_null(self::$rootPage) )
        {
            self::$rootPage = self::GetLngPageInfo("Index");
        }
        return self::$rootPage;
    }

    public static function GetPageUrl( $pageId, $params = null )
    {
        $pageInfo = self::GetPageInfo($pageId);

        return $pageInfo ? $pageInfo->GetUrl($params) : '';
    }

    /**
     * Returns page info according to site language.
     * @param string $pageId
     * @return PageInfo
     */
    public static function GetLngPageInfo( $pageId )
    {
        $lng = Core::GetLanguage();
        $lngPageId = PageInfo::GetTranslatedPageId( $pageId, $lng );
        $pageInfo = self::GetPageInfo( $lngPageId );
        if ( empty( $pageInfo ) )
        {
            $pageInfo = self::GetPageInfo( $pageId );
        }
        return $pageInfo;
    }

    /**
     * @param string $pageId
     * @return PageInfo
     */
    public static function GetPageInfo( $pageId )
    {
        if ( isset(self::$pages[$pageId]) )
        {
            return self::$pages[$pageId];
        }

        $pageInfo = self::GetPageInfoFromCache( $pageId );

        if ( !empty($pageInfo) )
        {
            self::$pages[$pageId] = $pageInfo;
        }

        return $pageInfo;
    }

    public static function GetChainToPage( $id )
    {
        $chain = array();
        $pageInfo = self::GetPageInfo($id);
        if ( !empty($pageInfo) )
        {
            array_unshift($chain, $pageInfo->id);
            $rootPage = Pages::GetRootPage();
            while ( !is_null($parentPageId = $pageInfo->parent) )
            {
                $pageInfo = self::GetPageInfo($parentPageId);
                array_unshift($chain, $pageInfo->id);
                if ( $pageInfo == $rootPage )
                {
                    break;
                }
            }
        }
        return $chain;
    }

    public static function GetPageInfoByUrl( $url )
    {
        $page = null;

        $finder = new PageInfoByURLFinder();
        $pageInfo = $finder->Find( $url );
        return $pageInfo;
    }

    public static function IsPageSecure( PageInfo $pageInfo )
    {
        if ( $pageInfo->secure === true )
        {
            return true;
        }

        if ( $pageInfo->secure === false )
        {
            return false;
        }

        if ( $pageInfo->parent )
        {
            $parentPage = self::GetPageInfo( $pageInfo->parent );

            return self::IsPageSecure( $parentPage );
        }

        return false;
    }

    /**
     * @param string $pageId
     * @param bool $showBreadscrumbs
     * @return Page
     */
    public static function CreatePage( $pageId, $showBreadscrumbs = true )
    {
        $pageInfo = self::GetPageInfo($pageId);
        if ( empty($pageInfo) )
        {
            return null;
        }
        return Page::Create( $pageInfo, $showBreadscrumbs );
    }

    /**
     * Creates page by PageInfo
     *
     * @param PageInfo $pageInfo
     * @param bool $showBreadscrumbs
     * @return Page
     */
    public static function CreatePageByPageInfo( $pageInfo, $showBreadscrumbs = true )
    {
        return ( empty($pageInfo) ) ? null : Page::Create( $pageInfo, $showBreadscrumbs );
    }
    
    public static function GetQuickLinks( $pageId )
    {
        $quickLinks = array();

        // Retrieve quick links for the particular page
        // or if not specified then for its parent page
        while ( !is_null($pageId) )
        {
            $pageInfo = self::GetPageInfo($pageId);
            if ( !empty($pageInfo->quickLinks) )
            {
                $quickLinks = $pageInfo->quickLinks;
                break;
            }

            $pageId = $pageInfo->parent;
        }

        // if specific quick links not found,
        // get common quick links
        if ( empty($quickLinks) )
        {
            $pageInfo = self::GetRootPage();
            $quickLinks = $pageInfo->quickLinks;
        }

        return $quickLinks;
    }

    /**
     * Checks page info in cache by page id.
     * Gets page info from resource manager if it doesn't exist in cache and saces into cache.
     * @param string $pageId
     * @return PageInfo
     */
    private static function GetPageInfoFromCache( $pageId )
    {
        $cache = Core::Cache();
        $id = "pages/_page_$pageId";
        $pageInfo = $cache->Get($id);
        if ( empty($pageInfo) )
        {
            $pages = Core::ResourceManager()->GetPages();
            $pageInfo = isset($pages) && array_key_exists( $pageId, $pages ) ? $pages[$pageId] : null;
            if ( !empty($pageInfo) )
            {
                $cache->Set($id, $pageInfo);
            }
        }
        return $pageInfo;
    }
}

?>
